<?php

class User_control extends Admin_Controller {

	function __construct()
	{
		parent::__construct();	
		
		$this->aPageJs[] = array(
							'combine_js' => true,
							'base_href' => '/media/js',
							'js' => array('admin_access_rights'),
							'version' => '1.0'
		);	

		// build up our header_css array
		$this->aPageCss[] = array(
							'combine_css' => true,
							'base_href' => '/media/css',
							'css' => array('admin_access_rights'),
							'media' => 'all',
							'version' => '1.0'
		);
	}
	
	public function index()
	{		
		$this->load->model('db/users', 'users');
		$qUsers = $this->users->get();
		
		$this->aData['qUsers'] = $qUsers;
			
		$this->load->view('platform/'.$this->sPlatform.'/admin/access_rights/index', $this->aData);
	}
	
	
	
	public function sec_groups()
	{
		$this->index();
		
		$this->load->model('db/security_groups', 'security_groups');
		
		if ($this->oSecGroup->site_id !== null)
		{
			$this->security_groups->site_id = $this->oSecGroup->site_id;
		}
		
		$qSecGroups = $this->security_groups->get();
		
		$this->aData['qSecGroups'] = $qSecGroups;
		
		$this->load->view('platform/'.$this->sPlatform.'/admin/access_rights/sec_groups', $this->aData);
		
			
	}
	
	
	public function edit_sec_group()
	{
		$this->index();
		
		$aSecGroup = $this->uri->uri_to_assoc(4);
		
		$iGroupId = 0;
		
		if (array_key_exists('group_id', $aSecGroup) === true)
		{
			$iGroupId = (int) $aSecGroup['group_id'];
		}
		
		
		$this->aPageJs[] = '
						var iGroupId = '.$iGroupId.'
		';
		
		$this->load->library('access_rights');
		
		$qAdminApps = $this->access_rights->get_security_group_apps($iGroupId);
		
		$this->aData['qAdminApps'] = $qAdminApps;
		
		$this->load->model('db/security_groups', 'security_groups');
		
		$this->security_groups->group_id = $iGroupId;
		
		if ($this->oSecGroup->site_id !== null)
		{
			$this->security_groups->site_id = $this->oSecGroup->site_id;
		}
		
		$qSecGroup = $this->security_groups->get(array(), 1);
		
		$oSecGroup = $this->security_groups->get_object();
		
		if ($qSecGroup->num_rows() === 1)
		{
			$oSecGroup = $qSecGroup->row(0);
		}
		
		$this->aData['oSecGroup'] = $oSecGroup;
		$this->aData['iGroupId'] = $iGroupId;
		
		$this->load->view('platform/'.$this->sPlatform.'/admin/access_rights/edit_sec_group', $this->aData);
		
	}
	
	public function edit_user()
	{
		$aUser = $this->uri->uri_to_assoc(4);
		
		$iUserId = 0;
		
		if (array_key_exists('user_id', $aUser) === true)
		{
			$iUserId = (int) $aUser['user_id'];
		}
		
		
		$this->load->model('db/users', 'users');
		
		$this->users->clear();
		
		$this->users->user_id = $iUserId;
		
		$qUser = $this->users->get(1);
		
		$oUser = null;
		
		if ($qUser->num_rows() === 1)
		{
			$oUser = $qUser->row(0);
		}
		else 
		{
			$oUser = $this->users->get_object();	
		}
			
		$this->aData['oUser'] = $oUser;
			
		// now get the security groups
		$this->load->model('db/security_groups', 'security_groups');
			
		$aOptions = array();
		$aOptions[] = array('order_by' => array('group_name', 'ASC'));
			
		$qSecGroups = $this->security_groups->get($aOptions);
			
		$this->aData['qSecGroups'] = $qSecGroups;
			
			
			
		$this->load->model('db/views/user_security_group', 'vw_user_security_group');
			
		$this->vw_user_security_group->user_id = $iUserId;
			
		$qUserSecGroup = $this->vw_user_security_group->get(1);

		$iUserSecGroup = 0;
		
		if ($qUserSecGroup->num_rows() === 1)
		{
			$iUserSecGroup = $qUserSecGroup->row(0)->group_id;
		}
		
		$this->aData['iUserSecGroup'] = $iUserSecGroup;
			
		$this->load->view('platform/'.$this->sPlatform.'/admin/access_rights/edit_user', $this->aData);

	}
	
	
	
	public function ajax() 
	{
		$this->sSkinFile = 'ajax';
		
		$sFunc = $this->uri->segment(4, false);
		
		if (($sFunc !== false) && (method_exists($this, $sFunc) === true))
		{
			$this->$sFunc();
				
		}
		
		return;
	}
	
	
	
	private function save_sec_group()
	{
		$iGroupId = (int) $this->input->post('iGroupId', true, 0);	
		$sGroupName = $this->input->post('sGroupName', true, '');
		$sGroupPublish = $this->input->post('sGroupPublish', true, 'N');
		$sGroupApps = $this->input->post('sGroupApps', true, '');
		$sGroupAppsDel = $this->input->post('sGroupAppsDel', true, '');
		
		
		$this->load->model('db/security_groups', 'security_groups');
	
		if ($iGroupId !== 0)
		{
			$this->security_groups->group_id = $iGroupId;	
		}
		
		$this->security_groups->site_id = null;
		$this->security_groups->group_name = $sGroupName;
		$this->security_groups->group_master = 'Y';
		$this->security_groups->group_publish = $sGroupPublish;
		
		
		$iGroupId = $this->security_groups->save(false);
		
		
		$aGroupApps = array($sGroupApps);
		
		if (stristr($sGroupApps, ',') !== false)
		{
			$aGroupApps = explode(',', $sGroupApps);	
		}
		
		$aGroupAppsDel = array($sGroupAppsDel);
		
		if (stristr($sGroupAppsDel, ',') !== false)
		{
			$aGroupAppsDel = explode(',', $sGroupAppsDel);	
		}
		
		
		$this->load->model('db/security_group_apps', 'security_group_apps');

		foreach ($aGroupApps as $iAppId)
		{
			$this->security_group_apps->check_app_by_group($iGroupId, $iAppId, false);
		}	

		foreach ($aGroupAppsDel as $iAppId)
		{
			$this->security_group_apps->check_app_by_group($iGroupId, $iAppId, true);
		}	
		
		
		$this->aData['json_encode']['sResp'] = 'Y';
			
		$this->load->view('php_json_encode', $this->aData);
	}
	
	
	
	private function list_users()
	{
		$sLetter = $this->input->post('sLetter', true, 'A');
			
		
		$this->load->model('db/users', 'users');
		$aOptions[0] = array('like' => array('user_lname', $sLetter, 'after'));
			
 		$qUsers = $this->users->get($aOptions);
		
		$this->aData['qUsers'] = $qUsers;
		
		
		$sContent = $this->load->view('platform/'.$this->sPlatform.'/admin/access_rights/list_users', $this->aData, true);
		
		$sContent = $this->jsonify_string($sContent);
		
		$this->aData['json_encode'] = array('sContent' => $sContent);
		
		$this->load->view('php_json_encode', $this->aData);
	}
	
	
	
	private function save_edit_user()
	{
		$iUserId = (int) $this->input->post('iUserId', true, 0);
		$sUserFirstName = $this->input->post('sUserFirstName', true, '');
		$sUserLastName = $this->input->post('sUserLastName', true, '');
		$sUserEmail = $this->input->post('sUserEmail', true, '');
		$sUserPasswd = $this->input->post('sUserPasswd', true, '');
		$sUserPasswdChk = $this->input->post('sUserPasswdChk', true, '');
		$iGroupId = (int) $this->input->post('iGroupId', true, 0);
		
		
		$this->load->model('db/users', 'users');
		
		$this->users->clear();

		if ($iUserId !== 0)
		{
			$this->users->user_id = $iUserId;
			
			if (($sUserPasswd != '') && ($sUserPasswd === $sUserPasswdChk))
			{
			
				$this->users->user_passwd = md5($sUserPasswd);
			}

		} else {
			$this->users->user_passwd = md5($sUserPasswd);
			$this->users->user_datetime = 'd/m/y H:i';
			
			$sVarifier = random_string('unique');
			
			$this->users->user_varified = 0;
			$this->users->user_varifier = $sVarifier;
			
			//send confirmation email
			$this->load->library('email');
			
			$this->email->from('your@example.com', 'Library of Assets Site');
			$this->email->to($sUserEmail);
			
			$sMessage = "Hi".chr(10)."Please click the link below to varify your email address.".chr(10).chr(10).site_url("account/varify/".$sVarifier).chr(10).chr(10)."You will need to do this before you can use the site.";
			
			$this->email->subject('Please varify you email address');
			$this->email->message($sMessage);
			
			//$this->email->send();
		}
		
		$this->users->user_fname = $sUserFirstName;
		$this->users->user_lname = $sUserLastName;
		$this->users->user_email = $sUserEmail;
			
		$this->users->save();
		
		
		if ($iUserId === 0)
		{
			$iUserId = $this->db->insert_id();	
		}	
			
		$this->load->model('db/security_group_users', 'security_group_users');
		
		$this->security_group_users->user_id = $iUserId;
			
		$qSecGroupUser = $this->security_group_users->get(1);
			
		if ($qSecGroupUser->num_rows() === 1)
		{
			$this->security_group_users->clear();
			$this->security_group_users->group_user_id = $qSecGroupUser->row(0)->group_user_id;
		}
		
		$this->security_group_users->group_id = $iGroupId;
		$this->security_group_users->save();	
		
			
		$this->aData['json_encode'] = array('sContent' => $this->aMessages['admin_edit_user_saved']);
			
		$this->load->view('php_json_encode', $this->aData);
		
		
		
	}
}

?>